export default {
  name: 'tatisticalReport', // 报表
  data() {
    return {

    }
  },
  methods:{
    handleSelectionChange(val) {
      // console.log('val', val)
      const vm = this
      if(this.mergeCells){
        this.multipleSelection = []
        let result = []
        for( let i = 0; i < val.length; i++){
          if(result.indexOf(val[i]) === -1){
            let list = vm.listData
            // 是否小计
            if(vm.isSubtotal){
              result = result.concat(list.filter(t => {
                if(t[vm.mergeName] === val[i][vm.mergeName] || (t[vm.mergeName] == '合计' && t[vm.mergeName+'Name'] === val[i][vm.mergeName])){
                  return t
                }
              }))
            } else {
              result = result.concat(list.filter(t => t[vm.mergeName] === val[i][vm.mergeName]))
            }
          }
        }
        this.multipleSelection = result
      } else {
        this.multipleSelection = val;
      }
      // console.log('this.multipleSelection', this.multipleSelection)
    },
    // 小计
    //小合计，在指定行下插入合计行
    handleTable(Human) {
      const vm = this
      let doctorMap = {};
      for (let i = 0; i < Human.length; i++) {//找出相同名称的行数
        let mergeName = Human[i][this.mergeName];
        if (doctorMap[mergeName] !== undefined) {
          doctorMap[mergeName].push(i);
        } else {
          doctorMap[mergeName] = [i];
        }
      }
      // console.log('doctorMap', doctorMap)
      let keyArr = []
      //取出key并倒序，防止正序插入会影响行下标
      for (let k in doctorMap) {
        keyArr.unshift(k)
      }
      keyArr.forEach((ele, index) => {
        //定义需要插入的数据
        let sumObj = {
          [vm.tableMoreStr]: vm[vm.tableMoreStr],
          [vm.mergeName + 'Name']: ele,
          [vm.mergeName]: '合计',//需要把哪个字段变成合计
          // selection: '',//需要把哪个字段变成合计
          //....需要插入合计的列名字段，与后端返回字段一致
          // orderAmount: 0,
          // balance: 0
        };
        //循环计算相同名称的行数据，例如'中医科':[0,1,2,3] 共计算四行数据
        for (let i = 0; i < doctorMap[ele].length; i++) {
          const e = doctorMap[ele][i];
          //计算累加数据
          //......
          //例：
          // sumObj.orderAmount+= Human[e].orderAmount
          //与sumObj里的字段对应
          let resultObj = {
            ...Human[e]
          }
          if(vm.summaryList && vm.summaryList.length > 0){
            vm.summaryList.forEach((item)=>{
              sumObj = {
                ...sumObj,
                [item]: sumObj[item] || 0
              }
              sumObj[item] += Human[e][item]
            })
          }
          for(let path in resultObj){
            let val = resultObj[path]
            if(this.isTimestampNumber(val)){
              resultObj[path] += Human[e][path]
            } else {
              resultObj[path] = ''
            }
          }
          sumObj = {
            ...resultObj,
            ...sumObj,
          }
        }

        let lastIndex = doctorMap[ele][doctorMap[ele].length - 1] //找出相同科室最后一行插入合计数据
        Human.splice(lastIndex + 1, 0, sumObj); //插入到指定位置
        console.log('Human', Human)
      })
      return Human

    },
    groupBy(arr, key)  {
      let newArr = [...new Set(arr.map(i => i[this.mergeName]))]; // 去重的时候需要注意和普通数组不同
      // console.log('newArr', newArr);

      let list = [];
      newArr.forEach(i => {
        // list.push(arr.filter(t => t[key] === i));
        const filterList = arr.filter(t => t[key] === i)
        if(this.isSubtotal){
          list.push(this.handleTable(filterList))
        } else {
          list.push(filterList);
        }

      })
      // console.log("list", list);

      let mList = [];
      list.forEach((item) => {
        if(item&&item.length>0){
          item.forEach((i, index) => {
            mList.push({
              ...i,
              deptCount: (index==0 ? item.length : 0),
            })
          })
        }
      })
      // console.log("mList", mList);
      return mList
    },
    objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
      const vm = this;
      if(!vm.mergeCells) return
      // console.log('column', column)
      // console.log('row', row)
      // 合并名称
      // if (columnIndex === 0) {
      if (column.property == this.mergeName) {
        let deptCount = 0
        if(this.isSubtotal){
          deptCount = row.deptCount - 1
        } else {
          deptCount = row.deptCount
        }
        if (row.deptCount > 0) {
          return {
            rowspan: deptCount,
            colspan: 1,
          };
        } else {
          if(this.isSubtotal){
            if(row[this.mergeName] == '合计'){
              return {
                rowspan: 1,
                colspan: 1,
              };
            }
          }
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }

      if (column.property === "selection") {
        let deptCount = row.deptCount
        if (row.deptCount > 0) {
          return {
            rowspan: deptCount,
            colspan: 1,
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0,
          };
        }
      }
    },
    // 总计
    getSummaries(param) {
      const vm = this
      if(!this.showSummary) return
      const { columns, data } = param;
      let sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计';
          return;
        }
        const values = data.map(item => {
          // 是否小计
          if(vm.isSubtotal && item[vm.mergeName] == '合计'){
            return 0
          }
          // 自定义列表字段合计
          if(vm.tableMoreStr && !item[column.property]){
            const customItem = item[vm.tableMoreStr].filter((item)=>{
              return item.label == column.property
            })
            if(customItem && customItem.length > 0){
              const customValue = customItem[0].value || 0
              return Number(customValue)
            }
          }
          return Number(item[column.property])
        });
        // console.log('vm.summaryList', vm.summaryList)
        if(!vm.summaryList.includes(column.property)) return;
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (this.isTimestampNumber(value)) {
              // if(this.isTimestamp(value)) return prev
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        } else {
          sums[index] = '';
        }
      });

      return sums;
    },

  }
}
